const joint_material = new BABYLON.StandardMaterial(
    'joint_material',
    scene
);
const arm_left1_joint = new BABYLON.MeshBuilder.CreateSphere(
    'arm_left1_joint',
    {
        diameter: 1,
    },
    scene
);
const arm_right1_joint = new BABYLON.MeshBuilder.CreateSphere(
    'arm_right1_joint',
    {
        diameter: 1,
    },
    scene
);
const arm_left2_joint = new BABYLON.MeshBuilder.CreateSphere(
    'arm_left2_joint',
    {
        diameter: 1,
    },
    scene
);
const arm_right2_joint = new BABYLON.MeshBuilder.CreateSphere(
    'arm_right2_joint',
    {
        diameter: 1,
    },
    scene
);
const arm_left1 = new BABYLON.MeshBuilder.CreateCylinder(
    'arm_left1',
    {
        height: 1,
        diameter: 1,
        faceColors: [
            new BABYLON.Color4(1, 231 * (1 / 255), 218 * (1 / 255), 1),
            new BABYLON.Color4(1, 231 * (1 / 255), 218 * (1 / 255), 1),
            new BABYLON.Color4(1, 231 * (1 / 255), 218 * (1 / 255), 1),
        ]
    },
    scene
);
const arm_right1 = new BABYLON.MeshBuilder.CreateCylinder(
    'arm_right1',
    {
        height: 1,
        diameter: 1,
        faceColors: [
            new BABYLON.Color4(1, 231 * (1 / 255), 218 * (1 / 255), 1),
            new BABYLON.Color4(1, 231 * (1 / 255), 218 * (1 / 255), 1),
            new BABYLON.Color4(1, 231 * (1 / 255), 218 * (1 / 255), 1),
        ]
    },
    scene
);
const arm_left2 = new BABYLON.MeshBuilder.CreateCylinder(
    'arm_left2',
    {
        height: 1,
        diameter: 1,
        diameterTop: 1,
        diameterBottom: 0.7,
        faceColors: [
            new BABYLON.Color4(1, 231 * (1 / 255), 218 * (1 / 255), 1),
            new BABYLON.Color4(1, 231 * (1 / 255), 218 * (1 / 255), 1),
            new BABYLON.Color4(1, 231 * (1 / 255), 218 * (1 / 255), 1),
        ]
    },
    scene
);
const arm_right2 = new BABYLON.MeshBuilder.CreateCylinder(
    'arm_right2',
    {
        height: 1,
        diameter: 1,
        diameterTop: 1,
        diameterBottom: 0.7,
        faceColors: [
            new BABYLON.Color4(1, 231 * (1 / 255), 218 * (1 / 255), 1),
            new BABYLON.Color4(1, 231 * (1 / 255), 218 * (1 / 255), 1),
            new BABYLON.Color4(1, 231 * (1 / 255), 218 * (1 / 255), 1),
        ]
    },
    scene
);
const arm_left_relative = 5;
const arm_right_relative = -5;

joint_material.diffuseColor = new BABYLON.Color3(1, 231 * (1 / 255), 218 * (1 / 255));

arm_left1.position.x = arm_left_relative + 0.5;
arm_left1.rotation.x = 90 * (Math.PI / 180);
arm_left1.rotation.z = 90 * (Math.PI / 180);
arm_left1.scaling.y = controller.back_arm;

arm_left2.position.x = arm_left1.position.x + 1;
arm_left2.rotation.z = 90 * (Math.PI / 180);
arm_left2.scaling.y = controller.front_arm;

arm_right1.position.x = arm_right_relative - 0.5;
arm_right1.rotation.x = 90 * (Math.PI / 180);
arm_right1.rotation.z = 270 * (Math.PI / 180);
arm_right1.scaling.y = controller.back_arm;

arm_right2.position.x = arm_right1.position.x - 1;
arm_right2.rotation.x = 90 * (Math.PI / 180);
arm_right2.rotation.z = 270 * (Math.PI / 180);
arm_right2.scaling.y = controller.front_arm;

arm_left1_joint.material = joint_material;
arm_left2_joint.material = joint_material;
arm_right1_joint.material = joint_material;
arm_right2_joint.material = joint_material;

arm_left1_joint.position.x = arm_left_relative;
arm_left2_joint.position.x = arm_left2.position.x - 0.5;

arm_right1_joint.position.x = arm_right_relative;
arm_right2_joint.position.x = arm_right2.position.x + 0.5;

var arm = -1;
controller.$watch('front_arm', function (v) {
    v = parseInt(v);
    arm_left2.scaling.y = v;
    arm_right2.scaling.y = v;
    v /= 2;
    if (arm === -1 || arm === 0) {
        arm_left2.position.x = arm_left1.position.x + v + 0.5;
        arm_left2_joint.position.x = arm_left2.position.x - v;

        arm_right2.position.x = arm_right1.position.x - v - 0.5;
        arm_right2_joint.position.x = arm_right2.position.x + v;
        arm = 0;
    } else if (arm === 1) {
        arm_left2.position.x = arm_left1.position.x + v + 1.5;
        arm_left2_joint.position.x = arm_left2.position.x - v;

        arm_right2.position.x = arm_right1.position.x - v - 1.5;
        arm_right2_joint.position.x = arm_right2.position.x + v;
        arm = 0;
    }
    // console.log('arm_left2.position.x', arm_left2.position.x, 'arm_left2_joint.position.x', arm_left2_joint.position.x, 'arm_right2.position.x', arm_right2.position.x, 'arm_right2_joint.position.x', arm_right2_joint.position.x);
});
controller.$watch('back_arm', function (v) {
    v = parseInt(v);
    arm_left1.scaling.y = v;
    arm_right1.scaling.y = v;
    v /= 2;

    if (arm === -1 || arm === 1) {
        arm_left1.position.x = arm_left_relative + v;
        arm_left2.position.x = arm_left1.position.x + v + 0.5;
        arm_left2_joint.position.x = arm_left2.position.x - v;

        arm_right1.position.x = arm_right_relative - v;
        arm_right2.position.x = arm_right1.position.x - v - 0.5;
        arm_right2_joint.position.x = arm_right2.position.x + v;
        arm = 1;
    } else if (arm === 0) {
        arm_left1.position.x = arm_left_relative + v;
        arm_left2.position.x = arm_left1.position.x + v + 1.5;
        arm_left2_joint.position.x = arm_left2.position.x - v;

        arm_right1.position.x = arm_right_relative - v;
        arm_right2.position.x = arm_right1.position.x - v - 1.5;
        arm_right2_joint.position.x = arm_right2.position.x + v;
        arm = 1;
    }
    // console.log('arm_left1.position.x', arm_left1.position.x, 'arm_left2.position.x', arm_left2.position.x, 'arm_right1.position.x', arm_right1.position.x, 'arm_right2.position.x', arm_right2.position.x, 'arm_left2_joint.position.x', arm_left2_joint.position.x, 'arm_right2_joint.position.x', arm_right2_joint.position.x);
});
